package com.dingding.util;

import com.dingding.config.URLConstant;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserListbypageRequest;
import com.dingtalk.api.response.OapiUserListbypageResponse;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 用户信息
 * @author: zyj
 * @create: 2020-03-10 10:57
 **/
@Service
public class UserUtil {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AccessTokenUtil accessTokenUtil;

    /**
    * @Description: 根据部门id获取用户信息
    * @Param: [deptId]
    * @return: java.util.List
    * @Author: zyj
    * @Date: 2020/3/10
    */
    public List<OapiUserListbypageResponse.Userlist> getUserInfoListByDeptID(Long deptId, long pageNo, long pageSize){
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_USER_LISTPAGE);
            OapiUserListbypageRequest req = new OapiUserListbypageRequest();
            req.setDepartmentId(deptId);
            req.setOffset(PageUtil.offset(pageNo, pageSize));
            req.setSize(pageSize);
            req.setHttpMethod("GET");
            OapiUserListbypageResponse rsp = client.execute(req, accessTokenUtil.getToken(URLConstant.COMPANY));
            logger.info("根据部门id获取用户信息，deptId={},msg={}", deptId, rsp.getBody());
            long errorCode = rsp.getErrcode();
            if(errorCode==0){
                List<OapiUserListbypageResponse.Userlist> userlists = rsp.getUserlist();
                return userlists;
            }
        } catch (ApiException e) {
            logger.error("根据部门id获取用户信息，部门id{}", deptId, e);
        }

        return null;
    }

}
